<template>
  <div>
    <el-page-header @back="$router.back()" content="电影列表" style="margin-bottom: 20px"/>
    <el-form :model="searchFrom" ref="searchForm" inline>
      <el-form-item label="电影名" prop="movieName">
        <el-input placeholder="电影名" v-model="searchFrom.movieName" clearable/>
      </el-form-item>
      <el-form-item label="导演" prop="directorId">
        <el-select v-model="searchFrom.directorId" clearable filterable>
          <el-option v-for="director in directorList" :key="director.directorId" :value="director.directorId" :label="director.directorName"/>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="searchFrom.status" clearable>
          <el-option :value="1" label="可用"/>
          <el-option :value="2" label="禁用"/>
        </el-select>
      </el-form-item>
      <el-form-item label="评分" prop="rangeScore">
        <el-select v-model="searchFrom.rangeScore" clearable>
          <el-option value="1,2" label="1-2分"/>
          <el-option value="3,4" label="3-4分"/>
          <el-option value="5,6" label="5-6分"/>
          <el-option value="7,8" label="7-8分"/>
          <el-option value="9,10" label="9分以上"/>
        </el-select>
      </el-form-item>
      <el-form-item label="发布日期" prop="releaseDate">
        <el-date-picker
          v-model="releaseDate"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getDataList">查询</el-button>
        <el-button type="warning" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
    <div style="margin-bottom: 15px">
      <el-button @click="openWin()">添加</el-button>
    </div>
    <el-table :data="dataList" border>
      <el-table-column label="编号" prop="movieId" width="60"/>
      <el-table-column label="电影名" prop="movieName"/>
      <el-table-column label="主要演员" prop="actors"/>
      <el-table-column label="电影分类">
        <template slot-scope="scope">
          <el-tag style="margin: 3px;" v-for="category in scope.row.categoryList" :key="category.categoryId">{{category.categoryName}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="发布日期" prop="releaseDate"/>
      <el-table-column label="电影评分" prop="score" width="100"/>
      <el-table-column label="导演" prop="director.directorName"/>
      <el-table-column label="状态" prop="status" width="80">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status === 1" size="small" type="success" @click="changeStatus(scope.row.movieId,2)">可用</el-button>
          <el-button v-else type="danger" size="small" @click="changeStatus(scope.row.movieId,1)">禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="small" @click="getPhotoList(scope.row.movieId)">图片</el-button>
          <el-button type="primary" size="small" @click="openWin(scope.row.movieId)">修改</el-button>
          <el-button type="warning" size="small" @click="delData(scope.row.movieId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :page-size="searchFrom.limit" :current-page="searchFrom.page" :page-sizes="[5,10,30,50,100]" :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
    <MovieUpdate ref="MovieUpdate" @closeWin="getDataList"/>
    <el-dialog title="电影剧照" :visible.sync="visible">
      <el-carousel :interval="4000" type="card">
        <el-carousel-item v-for="item in photoList" :key="item.imageId">
          <el-image :src="baseImageSrc+item.imageSrc"></el-image>
        </el-carousel-item>
      </el-carousel>
    </el-dialog>
  </div>
</template>

<script>
import MovieUpdate from '@/views/movie/MovieUpdate'
export default {
  name: 'MovieList',
  components: { MovieUpdate },
  data () {
    return {
      searchFrom: {
        page: 1,
        limit: 5,
        status: '',
        rangeScore: '',
        directorId: '',
        movieName: '',
        rangeRelease: ''
      },
      dataList: [],
      total: 0,
      directorList: [],
      releaseDate: '',
      visible: false,
      photoList: [],
      baseImageSrc: process.env.VUE_APP_BASE_API + 'static/upload/'
    }
  },
  methods: {
    resetForm () {
      this.$refs.searchForm.resetFields()
      this.releaseDate = ''
    },
    delData (movieId) {
      this.$confirm(`确定删除编号[${movieId}]的值吗?`, { type: 'info' }).then(() => {
        this.$http.delete(`movie/delete/${movieId}`).then(() => {
          this.$alert('删除数据成功！', { type: 'success' }).then(() => {
            this.getDataList()
          })
        })
      })
    },
    openWin (movieId) {
      this.$refs.MovieUpdate.init(movieId)
    },
    getDataList () {
      this.searchFrom.rangeRelease = this.releaseDate?.toString()
      this.$http.get('movie/page', this.searchFrom).then(data => {
        this.dataList = [...data.data.list]
        this.total = data.data.total
      })
    },
    handleSizeChange (val) {
      this.searchFrom.limit = val
      this.getDataList()
    },
    handleCurrentChange (val) {
      this.searchFrom.page = val
      this.getDataList()
    },
    changeStatus (movieId, status) {
      this.$confirm(`确定更改编号[${movieId}]的状态为${status === 1 ? '[<span style="color: green">可用</span>]' : '[<span style="color: red">禁用</span>]'}吗？`, { dangerouslyUseHTMLString: true }).then(() => {
        this.$http.post('movie/status', { movieId, status }).then(() => {
          this.$message.success('更改状态完成！')
          this.getDataList()
        })
      })
    },
    getDirectorList () {
      this.$http.get('director/select').then(data => {
        this.directorList = [...data.data]
      })
    },
    getPhotoList (movieId) {
      this.$http.get(`movie/${movieId}/image`).then(data => {
        this.photoList = [...data.data]
        this.visible = true
      })
    }
  },
  created () {
    this.$nextTick(() => {
      this.getDataList()
      this.getDirectorList()
    })
  }
}
</script>

<style scoped>

</style>
